package gonz.common;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;

public class Finder {
    public static final Logger log = Logger.getLogger(Finder.class);
    public static final long waitInterval = 10;//sec

    public static WebElement waitAndFindElement(WebDriver webDriver, final By by) {
        WebElement webElement = (new WebDriverWait(webDriver, waitInterval))
                .until(new ExpectedCondition<WebElement>() {
                    @Override
                    public WebElement apply(WebDriver d) {
                        return d.findElement(by);
                    }
                });
        log.debug(by + " element has been found.");
        return webElement;
    }

    public static List<WebElement> waitAndFindElements(WebDriver webDriver, final By by) {
        List<WebElement> webElements = (new WebDriverWait(webDriver, waitInterval))
                .until(new ExpectedCondition<List<WebElement>>() {
                    @Override
                    public List<WebElement> apply(WebDriver d) {
                        return d.findElements(by);
                    }
                });
        log.debug(by + " elements has been found.");
        return webElements;
    }
}
